//
//  ZXXWelcomeController.swift
//  M-Weibo
//
//  Created by ZXX on 16/4/19.
//  Copyright © 2016年 ZXX. All rights reserved.
//

import UIKit

///欢迎回来Label距离头像的Margin
let welcomeTopToHeadImageView = CGFloat(16)
///头像距离上面高度
let headImageTopToView = CGFloat(400)
///动画后头像距离上面的高度
let headImageTopToViewAnim = CGFloat(100)
///用户头像宽高
let headImageViewWidth = CGFloat(85)
let headImageViewHeight = headImageViewWidth
///用户头像边框宽度
let borderWidth = CGFloat(2)
///欢迎回来的Label 内容
let welcomeText = "欢迎回来"
///字号大小
let welcomeTextSize = CGFloat(17)

class ZXXWelcomeController: UIViewController {
    // MARK: -  设置背景图片
    override func loadView() {
        view = bgImageView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    // MARK: -  添加头像动画
    override func viewDidAppear(animated: Bool) {
        setHeadImageViewAnim()
    }
    
    ///头像动画--阻尼动画
    private func setHeadImageViewAnim(){
        //更新我们headView的约束
        headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(headImageTopToViewAnim)
        }
        // 01 - 动画执行时长
        // 02 - 动画延迟多久执行
        // 03 - Damping 阻尼 (0-1) 阻尼越小弹性越大
        // 04 - Velocity 起始速度
        UIView.animateWithDuration(2, delay: 1, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
                //layout 刷新布局
            self.view.layoutIfNeeded()
            }) { (_ ) -> Void in
                //欢迎回来的label 动画显示
                UIView.animateWithDuration(0.25, animations: { () -> Void in
                   self.welcomeLabel.alpha = 1
                    }, completion: { (_ ) -> Void in
                        //发送通知给appDelegata 进行切换根视图
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchDefaultVcNSNotification, object: "welcome")
                })
        }
    }
    
    
    // MARK: -  设置视图
    private func setupUI(){
        //1.添加控件
        view.addSubview(headImageView)
        view.addSubview(welcomeLabel)
        //2.添加约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSize(width: headImageViewWidth, height: headImageViewHeight))
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(headImageTopToView)
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(welcomeTopToHeadImageView)
            
        }
        
    }
    
    // MARK: -  懒加载控件
    ///背景图片
    private lazy var bgImageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    ///用户头像
    private lazy var headImageView:UIImageView = {
       let headImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        headImageView.layer.cornerRadius = headImageViewWidth * 0.5
        headImageView.layer.borderColor = ThemeColor.CGColor
        headImageView.layer.borderWidth = borderWidth
        headImageView.clipsToBounds = true
        
        return headImageView
    }()
    ///欢迎回来的label
    private lazy var welcomeLabel: UILabel = {
        let welcomeLabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: welcomeTextSize)
        welcomeLabel.text = welcomeText
        welcomeLabel.textAlignment = .Center
        welcomeLabel.alpha = 0
//        welcomeLabel.backgroundColor = UIColor.orangeColor()
        return welcomeLabel
    }()
    
    
}
